using UnityEngine;

public class CheckpointController : MonoBehaviour
{
	public CoinsController CoinsController;

	public GameUI GameUI;

	public LevelSelection LevelSelection;

	private GameObject[] checkpoints;

	public int finish_level_bonus;

	[HideInInspector]
	public Transform arrow;

	private float smooth = 1f;

	[HideInInspector]
	public int counter;

	public bool allow_bonus;

	private int trigger_type;

	public static CheckpointController instance
	{
		get;
		private set;
	}

	private void Awake()
	{
		if (instance != null && instance != this)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		instance = this;
		GetCheckpoints();
		SetCheckpoints();
	}

	public static CheckpointController GetInstance()
	{
		return instance;
	}

	private void Update()
	{
		if (checkpoints.Length > 0 && arrow != null && counter < checkpoints.Length)
		{
			Vector3 forward = Vector3.RotateTowards(arrow.forward, checkpoints[counter].transform.position - arrow.position, smooth * Time.deltaTime, 0f);
			arrow.rotation = Quaternion.LookRotation(forward);
		}
	}

	private void GetCheckpoints()
	{
		checkpoints = GetFirstChildren(base.transform);
	}

	private GameObject[] GetFirstChildren(Transform parent)
	{
		Transform[] componentsInChildren = parent.GetComponentsInChildren<Transform>();
		GameObject[] array = new GameObject[parent.childCount];
		int num = 0;
		Transform[] array2 = componentsInChildren;
		foreach (Transform transform in array2)
		{
			if (transform.parent == parent)
			{
				array[num] = transform.gameObject;
				num++;
			}
		}
		return array;
	}

	private void SetCheckpoints()
	{
		int num = 0;
		GameObject[] array = checkpoints;
		foreach (GameObject gameObject in array)
		{
			if (num == 0)
			{
				gameObject.SetActive(value: true);
			}
			else
			{
				gameObject.SetActive(value: false);
			}
			num++;
		}
	}

	public bool CheckCheckpoint(GameObject o)
	{
		if (o == checkpoints[counter])
		{
			return true;
		}
		return false;
	}

	public void NextCheckpoint(GameObject o)
	{
		o.SetActive(value: false);
		counter++;
		if (counter < checkpoints.Length)
		{
			checkpoints[counter].SetActive(value: true);
			return;
		}
		allow_bonus = false;
		if (PlayerPrefs.GetInt("level_" + PlayerPrefs.GetInt("selected_level", 0) + "_awarded", 0) == 0)
		{
			CoinsController.AddCoins(finish_level_bonus);
			PlayerPrefs.SetInt("level_" + PlayerPrefs.GetInt("selected_level", 0) + "_awarded", 1);
			allow_bonus = true;
		}
		if (PlayerPrefs.GetInt("selected_level", 0) == LevelSelection.levels.Length - 1)
		{
			trigger_type = 2;
			if (allow_bonus)
			{
				GameUI.FinishGameDialogShow("关卡完成奖励:  " + finish_level_bonus);
			}
			else
			{
				GameUI.FinishGameDialogShow(string.Empty);
			}
		}
		else
		{
			trigger_type = 1;
			if (allow_bonus)
			{
				GameUI.FinishDialogShow("关卡完成奖励:  " + finish_level_bonus);
			}
			else
			{
				GameUI.FinishDialogShow(string.Empty);
			}
		}
	}
}
